
# 获取营业部排行榜

# 获取前25或者55个营业部的最近3个月的个股，和板块名称


# 主要模块
# 下载网页 requests
# josn 解码
# 数据库 mysql
# 结果展示sankey关系图 ，echarts

import requests
import json
import pymysql


def get_paihang(pnum=1,ynum=5):
    '''
    http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=LHB&sty=YYTJ&stat=6&sr=0&st=1&p=2&ps=50&js=var%20BTXigdop={%22data%22:[(x)],%22pages%22:%22(pc)%22,%22update%22:%22(ud)%22}&rt=50897252
    '''
    url = "http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=LHB&sty=YYTJ&stat=6&sr=0&st=1&p=1&ps="+str(pnum)+"&js={%22data%22:[(x)],%22pages%22:%22(pc)%22,%22update%22:%22(ud)%22}"
    # html = requests.get(url)
    # # print(html.text)
    # json_data = json.loads(html.text)
    # print(json_data['data'])

    dd = {'type':'LHB',
    'sty':'YYTJ',
    'stat':'3',
    'sr':'0',
    'st':'1',
    'p':'1',
    'ps':'50',
    'js':'var cudzKzWb={"data":[(x)],"pages":"(pc)","update":"(ud)"}'}
    data = get_josn(url,dd)

    for value in data:
        # print(value.split(','))
        ones = value.split(',')
        # print(ones)
        get_yingyebu(ones[0],ones[12],ynum)
        pass
    pass

def get_yingyebu(id,name,num):
    # http://data.eastmoney.com/soft/stock/TradeSearchHistory.aspx?id=80601499
    # http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=LHB&sty=ZLYYMM&stat=3&code=80601499&sr=0&st=2&p=1&ps=50&js=var%20mqaFKGQP={%22data%22:[(x)],%22pages%22:%22(pc)%22,%22update%22:%22(ud)%22}
    # url = "http://data.eastmoney.com/soft/stock/TradeSearchHistory.aspx?id=%s"%(id)
    url = 'http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=LHB&sty=ZLYYMM&stat=3&code='+str(id)+'&sr=0&st=2&p=1&ps='+str(num)+'&js={"data":[(x)],"pages":"(pc)","update":"(ud)"}'
    # print(url,name)
    # html = requests.get(url)
    # # print(html.text)
    # json_data = json.loads(html.text)
    # # print(json_data['data'])

    # da = {'some': 'data'}
    dd = {'type':'LHB',
        'sty':'ZLYYMM',
        'stat':'3',
        'code':str(id),
        'sr':'0',
        'st':'2',
        'p':'1',
        'ps':'50',
        'js':'var mqaFKGQP={"data":[(x)],"pages":"(pc)","update":"(ud)"}',
        }
    data = get_josn(url,dd)
    # print(data)


    for value in data:
        ones = value.split(',')
        # print(ones)
        print(ones[2],ones[6],ones[10],ones[11],ones[12],ones[13])
        pass
    pass

def get_josn(arg,data):
    data = data
    headers = {'Accept': '*/*',
               'Accept - Encoding':'gzip,deflate',
               'Accept-Language':'zh-CN',
               'DNT':'1',
               'Connection':'Keep-Alive',
               'Host':'datainterface.eastmoney.com',
               'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063',
               'X-DevTools-Emulate-Network-Conditions-Client-Id':'0E5E805F-8F9C-433A-86E3-83DCD8D11732'
               }
    html = requests.get(arg,data=data,headers=headers, timeout=15)
    # print(html.text)
    json_data = json.loads(html.text)
    # print(json_data['data'])


    data = json_data['data']

    return data
    # pass

def make_echarts(arg):
    pass


if __name__ == '__main__':
    get_paihang(25,255)
